package com.example.magicapi.controller;

import com.example.magicapi.service.CustomService;
import com.example.magicapi.util.RestResult;
import com.google.common.base.Splitter;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import java.util.*;

/**
 * @author zk
 */
@RestController
public class CustomController {
    @Resource
    private CustomService service;
    @RequestMapping(value = "/template/cdStructuredCustom/customEmrList/query", method = RequestMethod.GET)
    public RestResult getCustomListData(@RequestParam Map<String, Object> params) throws Exception {
        List<Map<String, Object>> customList = service.getCustomList(params);
        return new RestResult(200, "查询成功", "rest",customList);
    }
    @RequestMapping(value = "template/cdStructuredCustom", method = RequestMethod.POST)
    public RestResult addCdStructuredCustom(@RequestBody Map<String, Object> params) throws Exception {
        return service.addCdStructuredCustom(params);
    }
    @RequestMapping(value = "template/cdStructuredCustom", method = RequestMethod.PUT)
    public RestResult updateCdStructuredCustom(@RequestBody Map<String, Object> params)
            throws Exception {
        if(doDetail(params)!=0) {
            return new RestResult(-1, "fialure", "",null);
        }
        return service.updateCdStructuredCustom(params);
    }


    public int doDetail(Map<String, Object> params)throws Exception {
        String customId=params.get("customId").toString();
        RestResult result = service.deleteCdStructuredCustomDetail(customId);
        if(result.getState()==200){
            if(!params.containsKey("pStr")) return 0;
            String pStr = params.get("pStr").toString();
            List<String> ss = Splitter.on("~").trimResults().splitToList(pStr);
            for(String subStr:ss){
                List<String> subSs=Splitter.on("`").trimResults().splitToList(subStr);
                Map<String,Object> map=new HashMap<>();
                map.put("customId", customId);
                map.put("sort", subSs.get(0));
                map.put("templateId", subSs.get(1));
                map.put("ext1", subSs.get(2));
                String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                map.put("detailId", uuid);
                service.addCdStructuredCustomDetail(map);
            }
            return  0;
        }else{
            return -1;
        }
    }


    @RequestMapping(value = "template/cdStructuredCustom/justPut", method = RequestMethod.PUT)
    public RestResult justUpdateCdStructuredCustom(@RequestBody Map<String, Object> params)
            throws Exception {
        return service.updateCdStructuredCustom(params);
    }
    @RequestMapping(value = "template/cdStructuredCustom/judgeIfExistSameShortName", method = RequestMethod.GET)
    public RestResult judgeIfExistSameShortName(@RequestParam Map<String, Object> params) {
        return service.judgeIfExistSameShortName(params);
    }
    @RequestMapping(value = "/template/cdStructuredCustomDetail/findByCondition", method = RequestMethod.GET)
    public RestResult findByCondition(@RequestParam Map<String, Object> params) {
        return service.findByCondition(params);
    }

    @RequestMapping(value = "/template/cdStructuredTemplate/getCustomList", method = RequestMethod.GET)
    public RestResult getCustomList(@RequestParam Map<String, Object> params) throws Exception {
        params.put("del", "0");
        params.put("templateType","999");
        List<Map<String, Object>> resultList = new ArrayList<>();
        resultList.addAll((List<Map<String, Object>>) service.getCdStructuredTemplate(params).getResults().get("cdStructuredTemplate"));
        params.put("shareRange", params.get("customId"));
        params.put("templateType","666");
        resultList.addAll((List<Map<String, Object>>) service.getCdStructuredTemplate(params).getResults().get("cdStructuredTemplate"));
        return new RestResult(200, "查询成功", "resultList",resultList);
    }
    @RequestMapping(value = "/template/cdStructuredTemplate/addHtmlTemplate", method = RequestMethod.POST)
    public RestResult addHtmlTemplate(@RequestBody Map<String, Object> params) throws Exception {
        return service.addHtmlTemplate(params);
    }
    @RequestMapping(value = "/template/cdStructuredTemplate/getList", method = RequestMethod.GET)
    @ApiOperation(value = "根据templateType得到该类别下的有效的结构化模板", notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "templateType", value = "结构化模板类别  0：病程记录 99：其他 100：护理记录模板 101：护理专科护理 102：护理措施", required = true, paramType = "query")
    })
    public RestResult getList(@RequestParam @ApiIgnore Map<String, Object> params) throws Exception {
        params.put("del", "0");
        RestResult result = service.getCdStructuredTemplate(params);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("resultList", result.getResults().get("cdStructuredTemplate"));
        return new RestResult(200,"查询成功",map);
    }
    @RequestMapping(value = "/template/cdStructuredTemplate/updateHtmlTemplate",method = RequestMethod.PUT)
    public RestResult updateHtmlTemplate(@RequestBody Map<String, Object> params) throws Exception {
        return service.updateHtmlTemplate(params);
    }
//    @RequestMapping(value = "deleteHtmlTemplate",method = RequestMethod.DELETE)
//    public RestResult deleteHtmlTemplate(@RequestBody Map<String, Object> params) throws Exception {
//        return service.deleteHtmlTemplate(params);
//    }

//    /template/cdStructuredCustomDetail/batchAdd

    @RequestMapping(value = "/template/cdStructuredCustomDetail/batchAdd",method=RequestMethod.POST)
    public RestResult batchAddCdStructuredCustomDetail(@RequestBody Map<String, Object> params) throws Exception {
        return service.batchAddCdStructuredCustomDetail(params);
    }
}

